﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace YJY_DMZS.BasisFile
{
    public partial class FrmDepartment : XtraForm
    {
        public FrmDepartment()
        {
            InitializeComponent();
            InitGrid();
        }

        private void FrmDepartment_Load(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            LoadDepartmentType();
            LoadDepartment(0);
        }
        /// <summary>
        /// 加载部门
        /// </summary>
        /// <param name="TypeID"></param>
        private void LoadDepartment(int TypeID)
        {
            if (tlsDepartmentType.FocusedNode != null)
            {
                TypeID = Convert.ToInt32(tlsDepartmentType.FocusedNode["ID"]);
            }
            else
            {
                TypeID = 0;
            }
            var bll = new DMZS.BLL.DepartmentNew();
            var ds = bll.GetListForUIExpand(TypeID, string.Empty);
            if (ds != null && ds.Tables.Count > 0)
            {
                grdDepartment.DataSource = ds.Tables[0];
            }
        }
        /// <summary>
        /// 加载部门类型
        /// </summary>
        private void LoadDepartmentType()
        {
            var bll = new DMZS.BLL.DepartmentType();
            var ds = bll.GetListForUIExpand(string.Empty);
            tlsDepartmentType.DataSource = ds.Tables[0];
        }
        private void btnNew_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var frm = new FrmDepartment_Editor();
            frm.ID = 0;
            frm.ShowDialog();
        }
        private void btnModify_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvDepartment.GetFocusedDataRow() != null)
            {
                var frm = new FrmDepartment_Editor();
                frm.ID = Convert.ToInt32(grvDepartment.GetFocusedDataRow()["ID"]);
                frm.ShowDialog();
            }
        }
        private void btnDelete_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (grvDepartment.GetFocusedDataRow() == null)
            {
                return;
            }
            if (DialogResult.Yes == XtraMessageBox.Show("确认删除?", Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
            {
                var bll = new DMZS.BLL.DepartmentNew();
                bll.Delete(Convert.ToInt32(grvDepartment.GetFocusedDataRow()["ID"].ToString()));

                if (tlsDepartmentType.FocusedNode != null)
                {
                    LoadDepartment(Convert.ToInt32(tlsDepartmentType.FocusedNode["ID"]));
                }
            }
        }
        private void btnRefresh_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        private void tlsDepartmentType_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            if (tlsDepartmentType.FocusedNode != null)
            {
                LoadDepartment(Convert.ToInt32(tlsDepartmentType.FocusedNode["ID"]));
            }
        }

        /// <summary>
        /// 初始网格显示
        /// </summary>
        private void InitGrid()
        {
            grvDepartment.IndicatorWidth = grvDepartment.IndicatorWidth == -1 ? 40 : grvDepartment.IndicatorWidth;
            grvDepartment.OptionsView.ShowIndicator = true;

            CommonControl.DxGridHelper.InitGridStyle(grvDepartment);
            CommonControl.DxGridHelper.InitGridStyle(tlsDepartmentType);
        }

        private void grvDepartment_DoubleClick(object sender, EventArgs e)
        {
            if (grvDepartment.GetFocusedDataRow() != null)
            {
                var frm = new FrmDepartment_Editor();
                frm.ID = Convert.ToInt32(grvDepartment.GetFocusedDataRow()["ID"]);
                frm.ShowDialog();
            }
        }

        private void grvDepartment_CustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            CommonControl.DxGridHelper.DrawRowIndicator(sender, e);
        }
    }
}
